var wolframAlphaGoogle_iframeBuster = {

	targetUrl: "",
	doc: "",

	checkIfUrlHasFrameBuster: function (targetUrl, originalPage) {
		//wolframAlphaGoogle_iframeBuster.targetUrl = targetUrl;
		//wolframAlphaGoogle_iframeBuster.doc = doc;
		//Create a unique outer iframe for each page that loads.
		var outerIframe = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","iframe");
		var outerIframeId = document.getElementById("outerIframe1");
		var IdNumber = 1;
		while (outerIframeId){
			IdNumber++;
			var outerIframeId = document.getElementById("outerIframe" + IdNumber);
		}
		outerIframe.setAttribute('id', "outerIframe" + IdNumber);
		outerIframe.setAttribute('style', 'display:none;');
		outerIframe.setAttribute('type', 'content');
		outerIframe.setAttribute('target-url', targetUrl);
		outerIframe.setAttribute('original-url', originalPage);
		document.getElementById("appcontent").setAttribute("orient", "horizontal");
		document.getElementById("appcontent").appendChild(outerIframe);
		outerIframe.addEventListener("load", wolframAlphaGoogle_iframeBuster.outerIframeLoaded, true);
	},

	outerIframeLoaded: function(event){
		var outerIframeId = event.target.defaultView.top.name;
		var outerIframe = document.getElementById(outerIframeId);
		var targetUrl = outerIframe.getAttribute("target-url");
		var innerIframe = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","iframe");
		innerIframe.setAttribute('id', 'innerIframe');
		innerIframe.setAttribute('style', 'display:none;');
		innerIframe.setAttribute('type', 'content');
		innerIframe.setAttribute('src', targetUrl);
		//alert (targetUrl)
		outerIframe.contentDocument.body.appendChild(innerIframe);
		outerIframe.removeEventListener("load", wolframAlphaGoogle_iframeBuster.outerIframeLoaded, true);
		outerIframe.addEventListener("load", wolframAlphaGoogle_iframeBuster.outerIframeReloaded, true);
	},

	outerIframeReloaded: function(event){
		var outerIframeId = event.target.defaultView.top.name;
  	var outerIframe = document.getElementById(outerIframeId);
		var targetUrl = outerIframe.getAttribute("target-url");
		var originalPage = outerIframe.getAttribute("original-url");
		var targetDocuments = wolframAlphaGoogle_iframeBuster.getDocumentFromUrl(originalPage);
  	outerIframe.removeEventListener("load", wolframAlphaGoogle_iframeBuster.outerIframeReloaded, true);
  	var innerIframe = outerIframe.contentDocument.getElementById("innerIframe");
		//alert (targetDocument.location + targetUrl);
  	if (innerIframe) {
  		//--com.sidthemonkey.Utils.reportInfo("No frame buster");
			//alert(event.target.documentElement.innerHTML);
			for (var i = 0; i < targetDocuments.length; i++) {
		  	wolframAlphaGoogle.loadWolframInIframe(targetDocuments[i], targetUrl, event.target.documentElement);
		  }
  	}
  	else {
  		com.sidthemonkey.Utils.reportInfo("Wolfram Alpha is using a Frame Buster. Not loading into main page.")
			for (var i = 0; i < targetDocuments.length; i++) {
		  	wolframAlphaGoogle_iframeBuster.wolframHasAFrameBuster(targetDocuments[i]);
		  }
  	}
		wolframAlphaGoogle_iframeBuster.removeFrames(outerIframeId);
  },
	
	getDocumentFromUrl: function (originalPage) {
		var num = gBrowser.browsers.length;
		var targetDocuments = [];
		for (var i = 0; i < num; i++) {
		  var b = gBrowser.getBrowserAtIndex(i);
		  try {
		    if (b.currentURI.spec == originalPage){
					targetDocuments.push(b.contentDocument);
				}
		  } catch(e) {
		    Components.utils.reportError(e);
		  }
		}
		//alert (targetDocuments[0]);
		return targetDocuments;
	},
	
	wolframHasAFrameBuster: function (mainDoc) {
		var wolframResult = "wakaranai";
		mainDoc.getElementById("wolfram-button").style.setProperty('background-image','url(chrome://wolframgoogle/content/resources/' + wolframResult + '.png)',null);
		mainDoc.getElementById("wolfram-button").title = "Wolfram Alpha tried to redirect the Google page to the Wolfram Alpha site. Not showing any results due to this.";
	},

	removeFrames: function (outerIframeId){
		var outerIframe = document.getElementById(outerIframeId);
		outerIframe.parentNode.removeChild(outerIframe);
		//document.getElementById("appcontent").removeAttribute("orient");
	},

}